import Common from '../common'
import cityTencent from '../../utils/cityTencent.json'
var Js = Common('merchant')
export default {
  name: 'merchant',
  mixins: [Js],
  components: {},
  data() {
    return {
      fields: [{
          key: 'user_no',
          label: 'NO',
          application: [],
          componentName: 'sls-input',
          listType: ''
        },
        {
          key: 'password',
          label: '密码',
          componentName: 'sls-input',
          dialog: {
            type: 'password'
          }
        },
				{
					key: 'login_name',
					label: '登录手机号',
					application: ['账户管理', '添加'],
					componentName: 'sls-input',
					listType: 'normal',
					placeholder: '请输入登录手机号',
					formatter:function(val){
						return val.user.login_name
					},
					header_search: {
						componentName: 'sls-input',
						style: 'width:160px;margin-right:2px;',
						placeholder: '请输入登录手机号',
						changeFunc: function(val, self) {
							if (val) {
								self.searchItem.login_name = ['LIKE', ['%' + val + '%']];
							} else {
								delete self.searchItem.login_name;
							};
						},
					}
				},
				{
					key: 'password',
					label: '密码',
					application: ['账户管理', '添加'],
					componentName: 'sls-input',
					dialog: {
						type: 'password'
					}
				},
        {
          key: "shop_name",
          label: '店铺名称',
          application: ['编辑', '添加'],
          componentName: 'sls-input',
          listType: 'normal',
          placeholder: '请输入店铺名称',
          header_search: {
            componentName: 'sls-input',
            style: 'width:160px;margin-right:2px;',
            placeholder: '请输入店铺名称',
            changeFunc: function(e, self) {
              if (e) {
                self.searchItem.shop_name = ['LIKE', ['%' + e + '%']]
              } else {
                delete self.searchItem.shop_name
              };
            },
          },
        },
        {
          key: "phone",
          label: '联系电话',
          application: ['编辑', '添加'],
          componentName: 'sls-input',
          listType: 'normal',
          header_search: {
            componentName: 'sls-input',
            style: 'width:160px;margin-right:2px;',
            placeholder: '请输入联系电话',
            changeFunc: function(e, self) {
              if (e) {
                self.searchItem.phone = ['LIKE', ['%' + e + '%']]
              } else {
                delete self.searchItem.phone
              };
            },
          },
        },
        {
          key: "time",
          label: '营业时间',
          application: ['编辑', '添加'],
          componentName: 'sls-input',
          listType: 'normal',
        },
        {
          key: 'label_str',
          label: '类别',
          application: ['编辑', '添加'],
          componentName: 'sls-select',
          optionsName: 'labelOptions',
          listType: 'normal',
          limit: 10,
          defaultProps: {
            label: 'title',
            value: 'id',
          },
          formatter: function(val) {
            return val.LabelName.title
          },
        },
        {
          key: "isShow",
          label: '首页展示',
          application: ['编辑', '添加'],
          componentName: 'sls-select',
          optionsName: 'isShowOptions',
          defaultProps: {
            label: 'text',
            value: 'value',
          },
          formatter: function(val) {
            if (val.isShow == 1) {
              return '是';
            } else {
              return '否';
            };
          },
          listType: 'normal',
        },

        {
          key: "region",
          label: '地区',
          application: ['编辑', '添加'],
          componentName: 'sls-cascader',
          optionsName: 'cityOptions',
          listType: 'normal',
          defaultProps: {
            children: 'children',
            label: 'fullname',
            value: 'fullname',
            multiple: true //此参数非常重要，多选时候传递
          },
          listType: 'normal',
        },
        {
          key: "address",
          label: '详细地址',
          application: ['编辑', '添加'],
          componentName: 'sls-input',
          listType: 'normal',
        },
        {
          key: 'map',
          label: '经纬度',
          application: ['编辑', '添加'],
          componentName: 'tencent-map',
          listType: '',
          dialogStyle: 'width:100%',
          changeFunc: function(value, self) {
            self.submitData.longitude = value['lng'];
            self.submitData.latitude = value['lat'];
            self.formData.longitude = value['lng'];
            self.formData.latitude = value['lat'];
          },
        },
        {
          key: 'longitude',
          label: '经度',
          application: ['编辑', '添加'],
          componentName: 'sls-input',
          listType: '',
        },
        {
          key: 'latitude',
          label: '纬度',
          application: ['编辑', '添加'],
          componentName: 'sls-input',
          listType: '',
        },
        {
          key: 'mainImg',
          label: '头像',
          customSlot: 'mainImg',
          application: ['编辑', '添加'],
          componentName: 'upload',
          listType: 'normal',
          limit: 10,
          dialogStyle: 'width:90%',
          width: 100
        },

        {
          key: 'create_time',
          label: '创建时间',
          listType: 'normal',
          placeholder: '请选择创建时间',
          width: 180,
          header_search: {
            componentName: 'sls-date-time-range',
            style: 'width:160px;margin-right:2px;',
            start_placeholder: '创建开始时间',
            end_placeholder: '创建结束时间',
            changeFunc: function(value, self) {
              if (!value) {
                delete self.searchItem.create_time;
              } else {
                self.searchItem.create_time = ['between', value = value.map(function(e) {
                  return e / 1000;
                })]
              };
            },
          },
        },
        {
          key: "status",
          label: '审核状态',
          application: ['编辑'],
          componentName: 'sls-select',
          optionsName: 'statusOptions',
          defaultProps: {
            label: 'text',
            value: 'value',
          },
          formatter: function(val) {
            var arr = ['已审核','待审核','拒绝']
            return arr[val.status-1];
          },
          listType: 'normal',
          header_search: {
            componentName: 'sls-select',
            optionsName: 'statusOptions',
            style: 'width:160px;margin-right:2px;',
            placeholder: '请选择审核状态',
            changeFunc: function(val, self) {
              console.log('header_search', val)
              if (val) {
                self.searchItem.status = val;
              } else {
                delete self.searchItem.status;
                console.log('self.searchItem', self.searchItem)
              };

            },
          }
        },
        {
          label: '操作',
          listType: 'deal',
          width: 190
        },
      ],
      // 按钮配置
      btn_info: [{
          type: 'info',
          icon: 'edit',
          size: 'mini',
          position: 'list',
          text: function(data) {
            return '编辑'
          },
          func: {
            apiName: function(self) {
              return "api_shopUpdate"
            },
            formData: function(self) {

              return self.formData
            },
            postData: function(self) {
              var postData = {
                searchItem: {
                  id: self.formData.id
                },
                data: self.submitData
              }

              if (self.submitData.region) {
                self.submitData.region = self.submitData.region.join(',');
              };

              return postData;
            }
          },
        },
        {
          type: 'danger',
          icon: 'delete',
          size: 'mini',
          funcType: 'submit',
          position: 'header',
          text: function(data) {
            return '删除选中'
          },
          func: {

            apiName: function(self) {
              return "api_shopUpdate"
            },

            postData: function(self) {
              var deleteArray = [];
              for (var i = 0; i < self.selectionArray.length; i++) {
                deleteArray.push(self.selectionArray[i].id);
              };
              var postData = {
                searchItem: {
                  id: ['in', deleteArray],
                },
                data: {
                  status: -1
                }
              };
              return postData;
            }

          },
        },
        {
          type: 'info',
          icon: 'edit',
          size: 'mini',
          position: 'header',
          text: function(data) {
            return '添加'
          },
          func: {
            apiName: function(self) {
              return "api_shopAdd"
            },
            formData: function(self) {
              var data = {
                longitude: '',
                latitude: '',
                map: {},
                label_str: '',
                login_name: '',
                password: '',
              };
              return data
            },
            postData: function(self) {
              var postData = {
                data: self.submitData,
                needRoleAuth: 12,
              };

              if(self.submitData.login_name){
                postData.login_name = self.submitData.login_name;
                delete  postData.data.login_name;
              };

              if(self.submitData.password){
                postData.password = self.submitData.password;
                delete  postData.data.password;
              };

              if (self.submitData.region) {
                self.submitData.region = self.submitData.region.join(',');
              };

              return postData;
            }
          },
        },
        {
          type: 'info',
          icon: 'edit',
          size: 'mini',
          position: 'list',
          text: function(data) {
            return '店长管理'
          },
          funcType: 'func',
          func: {
            func: function(self) {
              self.$router.push({
                path: '/home/shopper',
                name: '店长管理',
                params: {
                  shop_no: self.originFormData.shop_no,
                  delKeepAlive: true,
                  backPath: '/home/shop',
                  path: '/home/shopper',
                }
              });
            },
          },
        },
        {
        	type: 'info',
        	icon: 'edit',
        	size: 'mini',
        	position: 'list',
        	text: function(data) {
        		return '账户管理'
        	},
        	styleFunc: function(data) {
        		return 'background-color: #e8b040;border-color:#e8b040'
        	},
        	func: {
        		apiName: function(self) {
        			return "api_userUpdate"
        		},
        		formData: function(self) {
        			return self.formData.user
        		},
        		postData: function(self) {
        			var postData = {
        				searchItem: {
        					user_no: self.formData.user_no,
        					role: 12
        				},
        				data: self.submitData
        			}
        			return postData;
        		}
        	},
        },
        {
          type: 'info',
          icon: 'edit',
          size: 'mini',
          position: 'list',
          text: function(data) {
            return '商家商品管理'
          },
          funcType: 'func',
          func: {
            func: function(self) {
              self.$router.push({
                path: '/home/shopProduct',
                name: '商家商品管理',
                params: {
                  shop_no: self.originFormData.shop_no,
                  user_no: self.originFormData.user_no,
                  delKeepAlive: true,
                  backPath: '/home/shop',
                  path: '/home/shopProduct',
                }
              });
            },
          },
        },
        {
          type: 'info',
          icon: 'edit',
          size: 'mini',
          position: 'list',
          text: function(data) {
            return '商品类别管理'
          },
          funcType: 'func',
          func: {
            func: function(self) {
              self.$router.push({
                path: '/home/shopInnerLabel',
                name: '商品类别管理',
                params: {
                  shop_no: self.originFormData.shop_no,
                  user_no: self.originFormData.user_no,
                  delKeepAlive: true,
                  path: '/home/shopInnerLabel',
                  backPath: '/home/shop',
                }
              });
            },
          },
        },

      ],

      searchItem: {
        status: ['in', [1, 2]]
      },
      startSearchItem: {
        status: ['in', [1, 2]]
      },
      optionData: {
        labelOptions: [],

        transportOptions: [{
          text: '免配送费',
          value: 1
        }, {
          text: '需要配送费',
          value: 0
        }],
        isShowOptions: [{
          text: '是',
          value: 1
        }, {
          text: '否',
          value: 0
        }],
        statusOptions: [{
          text: '已审核',
          value: 1
        }, {
          text: '待审核',
          value: 2
        },{
          text: '拒绝',
          value: 3
        }],
        behaviorOptions: [],
        cityOptions: [],
        provinceOptions: [],
        areaOptions: []
      },
      otherData: {},
      UserInfo: {
        tableName: 'UserInfo',
        searchItem: {},
        fixSearchItem: {
          status: 1
        },
        key: 'user_no',
        middleKey: 'user_no',
        condition: 'in',
      },
      user_info: this.$store.state.user.userinfo,
      cityData: []
    }

  },

  mounted() {
    const self = this;

    self.optionData.cityOptions = cityTencent;
    for (var i = 0; i < self.optionData.cityOptions.length; i++) {
      console.log('i', i)
      for (var j = 0; j < self.optionData.cityOptions[i].children.length; j++) {
        // delete self.optionData.cityOptions[i].children[j].children
      };
    };
    console.log('cityTencent', cityTencent);

    this.init()
  },
  computed: {
    token: function() {
      return this.$store.getters.getToken
    }
  },
  methods: {

    /**
     * 初始化
     */
    init() {
      if (this.user_info.role != 3) {
        this.searchItem.id = this.user_info.shop_no;
        this.startSearchItem.id = this.user_info.shop_no;
      }
      this.initMenuData();
      this.initMainData();
      // this.initMapData();
    },


    async initMapData() {
      const self = this;
      const postData = {};
      postData.searchItem = {
        type: 2,
      };
      postData.token = self.$store.getters.getToken;

      try {
        var res = await self.$$api_labelGet({
          data: postData
        });
      } catch (err) {};
      if (res.info.data.length > 0) {
        self.optionData.areaOptions = res.info.data;
      };

    },

    async initCityData() {
      const self = this;
      const postData = {};
      try {
        var res = await self.$$api_getTxDistrict({
          data: postData
        });
      } catch (err) {};
      self.optionData.provinceOptions = res.info.result[0];
      // console.log('self.optionData', self.optionData)
    },

    async initChildData(type, id) {
      const self = this;
      const postData = {
        id: id
      };
      try {
        var res = await self.$$api_getTxDistrictChildren({
          data: postData
        });
      } catch (err) {};
      if (type == 1) {
        self.optionData.cityOptions = res.info.result[0];
      } else {
        self.optionData.areaOptions = res.info.result[0];
      }
      // console.log('self.optionData', self.optionData)
    },


    async initMenuData() {
      const self = this;
      const postData = {};
      postData.searchItem = {
        type: 3,
      };

      try {
        var res = await self.$$api_labelGet({
          data: postData
        });
      } catch (err) {};
      if (res.info.data.length > 0) {
        self.optionData.labelOptions = res.info.data;
      };
      console.log('self.optionData', self.optionData)
    },

    // https://apis.map.qq.com/ws/district/v1/list?key=BXVBZ-4GJ6R-FFZWQ-WX6IQ-TG7SQ-35FEJ

    /**
     * 列表主函数
     */
    async initMainData() {
      const self = this;
      const postData = {};
      postData.paginate = self.$$cloneForm(self.paginate);
      postData.token = self.$store.getters.getToken;
      if (self.searchItem) {
        postData.searchItem = self.$$cloneForm(self.searchItem)
      };
      if (JSON.stringify(self.getBefore) != "{}") {
        postData.getBefore = self.$$cloneForm(self.getBefore);
      };
      postData.getAfter = {
        LabelName: {
          tableName: 'Label',
          middleKey: 'label_str',
          key: 'id',
          condition: '=',
          info: ['title'],
        },
				user: {
					condition: "=",
					key: "shop_no",
					middleKey: "shop_no",
					searchItem: {
						status: 1,

					},
					tableName: "User",
					info: ['login_name', 'password','user_no']
				},
      };
      var res = await self.$$api_shopGet({
        data: postData
      });
      if (res.info.data.length > 0) {
        res.info.data.forEach((item, index) => {
          item.area = [];
          item.area.push(item.city);
          item.area.push(item.district);
          item.label_str = item.label_str.split(',');
          for (var i = 0; i < item.label_str.length; i++) {
            item.label_str[i] = parseInt(item.label_str[i]);
          };
          item.map = {
            lat: item.latitude,
            lng: item.longitude,
          }
        });
      };

      self.mainData = res.info.data;

      for (var i = 0; i < self.mainData.length; i++) {
        self.mainData[i]['region'] = self.mainData[i]['region'].split(',');
      };


      self.paginate.count = res.info.total;
    },


  },


}
